package com.ychs.controller;


import com.ychs.bean.Menu;
import com.ychs.bean.RoleMenu;
import com.ychs.service.IMenuService;
import com.ychs.service.IRoleMenuService;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Description: 描述【菜单树】
 * Copyright: Copyright (c) 2018
 * Company: 英才汇硕信息技术有限公司
 *
 * @author tianchuanjie
 * @version 1.0
 * @created 2018/12/28 9:57
 */
@Controller
public class MenuZtreeController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleMenuService roleMenuService;


    @GetMapping("/menuZtree")
    public String menuZtreeInfo() {
        return "/admin/menuZtree";
    }

    /**
     * 加载菜单ztree
     *
     * @param response json数据
     */
    @GetMapping("/menuZtreeJsp")
    public void menuZtreeJsp(HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        //查询所有菜单列表
        List<Menu> menuList = menuService.query();
        //json数据和页面交互
        PrintWriter out = null;
        try {
            out = response.getWriter();
            JSONArray jsonArray = JSONArray.fromObject(menuList);
            System.out.print(jsonArray.toString());
            out.println(jsonArray.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击节点的时候显示的表格页面
     *
     * @param menuId 点击的菜单id
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @GetMapping("/menuInfo")
    public String school(int menuId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //查询节点的信息 是否有子菜单
        List<Menu> menuList = menuService.queryByPid(menuId);
        JSONArray jsonArray = JSONArray.fromObject(menuList);
        req.setAttribute("menuList", menuList);
        req.setAttribute("menuId", menuId);
        return "/admin/menuInfo";
    }

    /**
     * 增加菜单
     *
     * @param menu    菜单对象信息
     * @param session
     * @return
     */
    @PostMapping("/addPMenuInfo")
    public String addPMenuInfo(Menu menu, HttpSession session) {
        //增加父菜单  菜单名唯一
        boolean result = menuService.insert(menu);
        //根据menuName查到menuId
        Menu menu1 = menuService.queryMenuId(menu.getName());
        //得到菜单id
        int menuId = menu1.getMenuId();
        //将菜单id  和roleId 组合 增加到roleMenuInfo 中间表
        //超级管理员创建菜单的时候直接就增加在权限中
        //不是超级管理员的也要增加菜单的时候加在中间表中
        int roleId = Integer.parseInt(session.getAttribute("roleId").toString());
        if (roleId != 1) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            boolean result1 = roleMenuService.insert(roleMenu);
            logger.debug(result1);

            RoleMenu roleMenu1 = new RoleMenu();
            roleMenu1.setRoleId(1);
            roleMenu1.setMenuId(menuId);
            boolean result2 = roleMenuService.insert(roleMenu1);
            return "redirect:/menuZtree";
        } else {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            boolean result1 = roleMenuService.insert(roleMenu);
            return "redirect:menuZtree";
        }

    }

    /**
     * 一开始加载树的时候 iframe页面显示的信息
     *
     * @param request
     * @return 父菜单信息集合
     */
    @GetMapping("/PMenuInfo")
    public String PMenuInfo(HttpServletRequest request) {
        //查询pid=0 的菜单信息  父菜单信息
        List<Menu> menuList = menuService.queryPMenu();
        request.setAttribute("menuList", menuList);
        return "/admin/pMenuInfo";
    }


    /**
     * 删除菜单信息
     *
     * @param menuId 菜单id
     * @return
     */
    @GetMapping("/DelPMenuInfo")
    public String PMenuInfo(int menuId) {
        //根据pid=menuId 查询菜单集合 集合为空则是子节点 集合不为空则为父节点
        List<Menu> menuList = menuService.queryByPid(menuId);

        if (menuList.size() == 0) {
            //点击的是子节点  删除自己就可以
            //删除菜单表中的信息
            int result = menuService.deleteByMenuId(menuId);
            //删除中间表权限中可浏览菜单信息
            int result1 = roleMenuService.deleteByMenuId(menuId);
            return "redirect:PMenuInfo";
        } else {
            //点击的是父节点

            //删除菜单表中父节点信息
            int result = menuService.deleteByMenuId(menuId);
            //删除菜单表中父节点下的子节点信息  和权限表中的子节点信息
            for (Menu menu : menuList) {
                //遍历出子节点菜单id
                int menuId1 = menu.getMenuId();
                int result2 = menuService.deleteByMenuId(menuId1);
                int result3 = roleMenuService.deleteByMenuId(menuId1);
            }
            //删除权限表中父节点信息
            int result1 = roleMenuService.deleteByMenuId(menuId);
            return "redirect:PMenuInfo";
        }
    }

    /**
     * 更新菜单信息
     *
     * @param menu 菜单id
     * @return
     */
    @PostMapping("/updatePMenuInfo")
    public String updatePMenuInfo(Menu menu) {
        //更新父菜单信息
        boolean result = menuService.update(menu);
        return "redirect:PMenuInfo";

    }


    /**
     * 增加子菜单信息
     *
     * @param menu    子菜单信息
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/addSonMenuInfo")
    public String addSonMenuInfo(Menu menu, HttpServletRequest request, HttpSession session) {
        int roleId = Integer.parseInt(session.getAttribute("roleId").toString());

        if (roleId != 1) {
            //给自己增加子菜单  还得给超级管理员增加子菜单
            boolean result = menuService.insert(menu);
            int menuId = menu.getMenuPid();
            Menu menu1 = menuService.queryMenuId(menu.getName());
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menu1.getMenuId());
            boolean result1 = roleMenuService.insert(roleMenu);

            RoleMenu roleMenu1 = new RoleMenu();
            roleMenu1.setRoleId(1);
            roleMenu1.setMenuId(menu1.getMenuId());
            boolean result2 = roleMenuService.insert(roleMenu1);

            List<Menu> menuList = menuService.queryByPid(menuId);
            request.setAttribute("menuList", menuList);
            request.setAttribute("menuId", menuId);
            return "/admin/menuInfo";
        } else {
            boolean result = menuService.insert(menu);
            int menuId = menu.getMenuPid();
            Menu menu1 = menuService.queryMenuId(menu.getName());
            int menuId1 = menu1.getMenuId();

            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId1);
            boolean result1 = roleMenuService.insert(roleMenu);

            List<Menu> menuList = menuService.queryByPid(menuId);
            request.setAttribute("menuList", menuList);
            request.setAttribute("menuId", menuId);
            return "/admin/menuInfo";
        }
    }

    /**
     * 更新子菜单信息
     *
     * @param menu    菜单信息
     * @param request
     * @return
     */
    @PostMapping("/updateSonMenuInfo")
    public String updatePMenuInfo1(Menu menu, HttpServletRequest request) {
        boolean result = menuService.update(menu);
        List<Menu> menuList = menuService.queryByPid(menu.getMenuPid());
        request.setAttribute("menuList", menuList);
        return "/admin/menuInfo";

    }

    /**
     * 删除子菜单信息
     *
     * @param menuId  菜单id
     * @param menuPid 菜单pid
     * @param request
     * @return
     */
    @GetMapping("/DelSonMenuInfo")
    public String SonMenuInfo(int menuId, int menuPid, HttpServletRequest request) {

        //删除子菜单
        int result = menuService.deleteByMenuId(menuId);
        //删除中间表菜单
        int result1 = roleMenuService.deleteByMenuId(menuId);

        //根据pid查询页面剩余信息
        List<Menu> menuList = menuService.queryByPid(menuPid);
        request.setAttribute("menuList", menuList);
        return "/admin/menuInfo";
    }
}
